//
//  TSControllerTransition.m
//  自定义转场动画
//
//  Created by 唐铁山 on 2018/9/11.
//  Copyright © 2018年 唐铁山. All rights reserved.
//

#import "TSControllerTransition.h"
#import "ViewController.h"
#import "TSBlackViewController.h"

@interface TSControllerTransition()<CAAnimationDelegate>

@property (nonatomic, strong) id<UIViewControllerContextTransitioning> context;

@end

@implementation TSControllerTransition
// 设置动画的执行时间
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.8f;
}

// 具体动画执行
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    // 持有context对象
    _context = transitionContext;
    
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // 获取containerView
    UIView *containerView = [transitionContext containerView];
    
    // 获取需要做动画的View，并添加到containerView
    if (self.isPush) {
        [containerView addSubview:fromVC.view];
        [containerView addSubview:toVC.view];
    }
    else {
        [containerView addSubview:toVC.view];
        [containerView addSubview:fromVC.view];
    }
    
    // 动画按钮
    UIButton *button =  (UIButton *)[fromVC valueForKey:(self.isPush ? @"blackButton" : @"redButton")];
    
    // 绘制小圆
    UIBezierPath *smallPath = [UIBezierPath bezierPathWithOvalInRect:button.frame];
    
    // 绘制大圆
        // 获取中心点
    CGPoint centerPoint = button.center;
    
    // 获取半径
    CGFloat radius;
        // 获取垂直和水平方向上，离屏幕右边或者底部的距离
    CGFloat horizontalWidth = CGRectGetWidth(toVC.view.bounds) - centerPoint.x;
    CGFloat verticalHeight = CGRectGetHeight(toVC.view.bounds) - centerPoint.y;
    // 垂直方向上的边长
    CGFloat horizontalLength;
    // 水平方向上的边长
    CGFloat verticalLength;
    if (centerPoint.x > CGRectGetWidth(toVC.view.bounds) / 2.0f) {
        if (centerPoint.y < CGRectGetHeight(toVC.view.bounds) / 2.0f) {
            horizontalLength = centerPoint.x;
            verticalLength = verticalHeight;
        }
        else {
            horizontalLength = centerPoint.x;
            verticalLength = centerPoint.y;
        }
    }
    else {
        if (centerPoint.y < CGRectGetHeight(toVC.view.bounds) / 2.0f) {
            horizontalLength = horizontalWidth;
            verticalLength = verticalHeight;
        }
        else {
            horizontalLength = horizontalWidth;
            verticalLength = centerPoint.y;
        }
    }
    radius = sqrtf(pow(horizontalLength, 2) + pow(verticalLength, 2));
    
    UIBezierPath *bigCirclePath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    // 将path添加到Layer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = self.isPush ? bigCirclePath.CGPath : smallPath.CGPath;
    
    // 设置蒙版
    if (self.isPush) {
        toVC.view.layer.mask = shapeLayer;
    }
    else {
        fromVC.view.layer.mask = shapeLayer;
    }

    
    //添加动画
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"path";
    animation.delegate = self;
    animation.fromValue = self.isPush ? (__bridge id)smallPath.CGPath : (__bridge id)bigCirclePath.CGPath;
    [shapeLayer addAnimation:animation forKey:nil];
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    [_context completeTransition:YES];
    if (self.isPush) {
        UIViewController *toVC = [_context viewControllerForKey:UITransitionContextToViewControllerKey];
        toVC.view.layer.mask = nil;
    }
    else {
        UIViewController *fromVC = [_context viewControllerForKey:UITransitionContextFromViewControllerKey];
        fromVC.view.layer.mask = nil;
    }
}

@end
